const pluginapp = require('../../index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    day_num: 0,
    favorWords: [],
    cateList: [],
    todayReadWord:[]
  },
  getFavor() {
    var that = this;
    pluginapp.getFavorWord(that.data.userId).then(res => {
      console.log(res)
      this.setData({
        favorWords: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userId = options.userId
    var that = this;
    this.setData({
      userId
    }, () => {
      // that.getFavor();
    })
    // pluginapp.translationTo('nl','世界人民大团结万岁').then(res=>{
    //   console.log(res)
    // })
    wx.showLoading({
      title: '加载单词册',
    })
    pluginapp.getCateList().then(res => {
      wx.hideLoading();
      console.log(res)
      this.setData({
        cateList: res.data
      })
    })

  },
  favorCateTap() {
    if (this.data.favorWords.length == 0) {
      console.log('无收藏单词')
    } else {
      wx.setStorageSync('wordList', this.data.favorWords)
      wx.navigateTo({
        url: '../extend/word?cateId=favor' + '&userId=' + this.data.userId,
      })
    }

  },
  cateTap(e) {
    wx.showLoading({
      title: '获取单词册中~',
      mask: true
    })
    console.log(e)
    let sourceUrl = e.currentTarget.dataset.api;
    let cateId = e.currentTarget.dataset.id;
    wx.request({
      url: sourceUrl,
      success: res => {
        console.log(res)
        let resArr = res.data;
        let wordList = []
        resArr.forEach(function (ele, index) {
          wordList.push(ele.word)
        })
        wx.setStorageSync('wordList', wordList)
        wx.navigateTo({
          url: '../extend/word?cateId=' + cateId + '&userId=' + this.data.userId,
        })
      },
      fail: res => {
        console.log(res)
      },
      complete: res => {
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getFavor()
    let todayReadWord = wx.getStorageSync(pluginapp.TimeCodeYmd()+'_ReadWord') || []
    console.log(todayReadWord)
    this.setData({
      todayReadWord
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})